package com.pti.usermanager.service.impl;

import java.util.List;

import com.pti.usermanager.dao.po.MenuPo;
import org.apache.ibatis.annotations.Param;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pti.usermanager.dao.api.CompDao;
import com.pti.usermanager.dao.po.CompPo;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.CompService;
import com.pti.usermanager.web.dto.request.SaveCompRequest;
import com.pti.usermanager.web.dto.request.SaveDeptByCidAndDid;
import com.pti.usermanager.web.dto.request.UpdateCompRequest;
import com.pti.usermanager.web.dto.response.ResultResponse;

/**
 * @ClassName: CompServiceImpl
 * @Author: dongzhipeng
 * @Description: 单位业务实现层
 * @Date: 2019/1/18
 */
@Service
public class CompServiceImpl implements CompService {


    private static final Logger logger = LogManager.getLogger(CompServiceImpl.class);

    @Autowired
    private CompDao compDao;


    @Override
    public ResultResponse findAllByComdId(Long compId) {
        logger.debug("comdId:{}",compId);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(compDao.findAllByCompId(compId));
        return resultResponse;
    }

    @Override
    public ResultResponse findTreeByComp() {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(compDao.findTreeByComp());
        return resultResponse;
    }

    @Override
    public ResultResponse findAll(String compNm) {
        logger.debug("compNm:{}",compNm);
        ResultResponse resultResponse = new ResultResponse();
        List<CompPo> findAllComp = compDao.findAllComp(compNm);
        resultResponse.setData(findAllComp);
        return resultResponse;
    }
    @Override
    public ResultResponse saveComp(SaveCompRequest saveCompRequest) {

        logger.debug("saveCompRequest:{}",saveCompRequest.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            compDao.saveComp(saveCompRequest);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }



    @Override
    public ResultResponse updateComp(UpdateCompRequest updateCompRequest) {

        logger.debug("updateCompRequest:{}",updateCompRequest.toString());

        ResultResponse resultResponse = new ResultResponse();

        try{
            compDao.updateComp(updateCompRequest);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    /**
     * @Author dongzhipeng
     * @Description 删除单位
     * @Date 2019/1/17
     */
    @Override
    public ResultResponse delComp(Long compId) {

        logger.debug("compId:{}",compId);

        ResultResponse resultResponse = new ResultResponse();

        try{
            compDao.delComp(compId);
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    /**
     * @Author liumengyao
     * @Description 分配部门
     * @Date 2019/1/21
     */
    @Override
    public ResultResponse saveDeptByCidAndDid(SaveDeptByCidAndDid saveDeptByCidAndDid) {

        logger.debug("saveDeptByCidAndDid:{}",saveDeptByCidAndDid.toString());

        ResultResponse resultResponse = new ResultResponse();

        try{
            compDao.deleteDeptByCid(saveDeptByCidAndDid.getCompId());
            for(Long deptId:saveDeptByCidAndDid.getDeptId()){
                compDao.saveDeptByCidAndDid(saveDeptByCidAndDid.getCompId(),deptId);
            }
            return resultResponse;
        }catch (Exception e){
        	logger.error(e.getMessage(),e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }

}
